
#coding: utf-8
#!/usr/bin/python

import datetime
import MeCab
import sys
import string

class TextFilter(object):


	def __init__(self, pubDate):
		self.pubDate = pubDate
		self.termFreqDict = {}
		self.keywords = []


	def mecabSpliter(self, text):
		try:
    			tagger = MeCab.Tagger (" ")
    			node = tagger.parseToNode (text)

    			while node:
				# debug
				#print node.surface, "\t", node.feature

				partsOfSpeech = node.feature.split(",")[0]
				morpheme = node.surface

				if partsOfSpeech == '名詞':
					self.keywords.append(morpheme)
					self.termFreqDict.setdefault(morpheme,0)
					self.termFreqDict[morpheme] += 1
				# find next node
				node = node.next

			return self.keywords

		except RuntimeError, e:
    			print "RuntimeError:", e;
		

	def termFreq(self, term):
		#debug 
		#for term, cnt in self.termFreqDict.iteritems():
			#print term , "\t", cnt
		return self.termFreqDict[term]

	def textToVec(self, text):
		self.mecabSpliter(text)
		return self.termFreqDict

	def getPubDate(self):
		return self.pubDate

	def weightOfDoc(self,ramda):
		diff = datetime.date.today() - self.pubDate
		return ramda ** (diff.days)
		


